<template>
  <div>
    <el-form ref="importList" label-position="right" label-width="100px">
      <el-form-item
        :label="$t('kwFaqManagement.accessoryUpload') + ':'"
        prop="file"
      >
        <el-upload
          class="upload-demo"
          ref="uploadTableQa"
          accept=".xls,.xlsx"
          action=""
          :with-credentials="true"
          :http-request="uploadTableQahttpRequest"
          :auto-upload="false"
          limit="1"
          :header="uploadHeader"
        >
          <el-button
            slot="trigger"
            size="small"
            type="primary"
            style="margin-right: 20px"
          >
            {{ $t("kwFaqManagement.chooseFile") }}
          </el-button>
          <span slot="tip" class="el-upload__tip">
            {{ $t("kwFaqManagement.excelFilesTip") }}
          </span>
        </el-upload>
      </el-form-item>
      <el-form-item prop="upload">
        <el-button type="text" @click="downloadTemplate">
          {{$t("kwFaqManagement.uploadTemplate") }}
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import serviceList from "@/config/serviceList";
import {baseURL} from "@/api/common";
import {importExcel} from "@/api/kwKnowledgeCenter/kwSettings/kwAnswerViewManage/index.js";
import {mapGetters} from "vuex";
const { knowledge } = serviceList;
export default {
  data(){
    return{

    }
  },

  computed: {
    ...mapGetters(["botObj"]),
  },

  methods:{
    uploadTableQahttpRequest(param) {
      let fileObj = param.file; // 相当于input里取得的files
      // console.log(fileObj);
      // 检查文件类型
      const allowedExtensions = /(\.xls|\.xlsx)$/i;
      if (!allowedExtensions.exec(fileObj.name)) {
        this.$message({
          type: 'error',
          message: this.$t("kwIntentionManage.errFileUploadType"),
          // '请选择 XLSX, XLS 类型的文件！',
        });
        this.$emit("successImport", false);
        return;
      }

      if (!['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.ms-excel'].includes(fileObj.type)) {
        this.$message({
          type: 'error',
          message: '请选择 XLSX, XLS 类型的文件！',
        });
        this.$emit("successImport", false);
        return;
      }

      let fd = new FormData(); // FormData 对象
      let size = Number(fileObj.size / 1024 / 1024);
      fd.append("file", fileObj);
      fd.append("botId", this.botObj.id);
      if (size > 20) {
        this.$message({
          type: "warning",
          message: "文件大小不能超过20MB,请重新上传!",
        });
        this.$emit("successImport", false);
        return;
      }
      importExcel(fd).then(
        () => {
          this.closeFlag = true;
          this.$message({
            type: "success",
            message: "导入成功!",
          });
          this.$emit("successImport", true);
        },
        (error) => {
          this.$emit("successImport", false);
          window.console.log(error);
        }
      );
    },


    downloadTemplate(){
      window.open(`${baseURL}api/${knowledge}/huilan-km-answer-angle/excel/download`,'_self');
    },

    saveImport() {
      if (this.$refs.uploadTableQa.uploadFiles.length > 0) {
        this.$refs.uploadTableQa.submit();
      } else {
        this.$emit("successImport", false);
      }
    },
  },
}
</script>

<style scoped lang="scss">

</style>
